from scrapy.http.headers import Headers, _RawValueT
from collections.abc import Mapping
from typing import Any, AnyStr
from collections.abc import Iterable

class CustomHeaders(Headers, dict):
    def __init__(
        self,
        seq: Mapping[AnyStr, Any] | Iterable[tuple[AnyStr, Any]] | None = None,
        encoding: str = "utf-8",
    ):
        self.encoding: str = encoding
        super().__init__(seq)

    def normkey(self, key: AnyStr) -> bytes:  # type: ignore[override]
        """Normalize key to bytes"""
        return self._tobytes(key)

    def not_normkey_update(self, seq: Mapping[AnyStr, Any] | Iterable[tuple[AnyStr, Any]]):
        """
        向headers添加键值对，不执行 normkey
        :return:
        """
        seq = seq.items() if isinstance(seq, Mapping) else seq
        for k, v in seq:
            a = self.normvalue(v)
            self[self._tobytes(k)] = v

    def update_for_Headers(self, dict):
        for k, v in dict.items():
            self[k] = v